<template>
	 <!-- v-if="JSON.stringify(renewData)!='{}'" -->
	<view class="version flex alignCenter" v-if="JSON.stringify(renewData)!='{}'">
		<view class="font14 vMain">
			<image src="https://image.alhelp.net/uploads/20231104/966003032828a4fd2e7d71fe92abcc48.png" mode="widthFix"></image>
			<view class="bold font16">我有新版本咯！</view>
			<view class="content" v-if="renewData.content">{{renewData.content}}</view>
			<view class="noWidthBtn" @click="renew">{{downloadTip}}</view>
			<view class="font14 color666" @click="quit">{{renewData.isMustUpdate?'退出应用':'暂不升级'}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"uni-versionnew",
		props:{
			renewData:{
				type:Object,
				default(){
					return {}
				}
			}
		},
		data() {
			return {
				downloadTip:'立即升级'
			};
		},
		methods:{
			// 升级
			renew(){
				if(this.renewData.system=='android'){
					this.downloadTip='新版本下载中...'
					uni.downloadFile({
						url: this.renewData.url,//如果是热更新则是热更新地址，如果是整包更新就是整包地址
						success: (downloadResult) => {
							plus.runtime.install(
								downloadResult.tempFilePath, {
									force: true
								},
								function(upRes) {
									// 应用热重启，重新启动进入首页。
									setTimeout(()=>{
										console.log('upRes',upRes,downloadResult)
										plus.runtime.restart();
									},2000)
								},
								function(e) {
									uni.showToast({
										title: '安装失败',
										icon: 'none'
									})
								})
						},
						fail: (err) => {
							console.log(err)
							uni.showToast({
								title: '下载失败',
								icon: 'none'
							})
						}
					})
				}else{
					//TO DO ios直接跳appstore地址
					
				}
			},
			quit(){
				uni.showTabBar()
				if(this.renewData.isMustUpdate){
					plus.runtime.quit();
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.version{
	position: fixed;
	left:0;
	top:0;
	width: 100%;
	height: 100%;
	background: rgba(0, 0, 0, .4);
	z-index: 99999999;
	.vMain{
		background: #fff;
		width: 80%;
		// margin:300rpx auto 0;
		margin: 0 auto;
		border-radius: 20rpx;
		text-align: center;
		padding: 220rpx 30rpx 30rpx;
		box-sizing: border-box;
		position: relative;
		image{
			width: 300rpx;
			height: 300rpx;
			display: block;
			margin: 0 auto;
			position: absolute;
			left: 50%;
			top: -100rpx;
			margin-left: -150rpx;
		}
	}
	.noWidthBtn{
		border-radius: 20rpx;
		padding: 20rpx 0;
		margin: 30rpx 0;
	}
}
.content{
	max-height: 300rpx;
	overflow-y: scroll;
	margin: 20rpx 0 0;
}
</style>